
var path = require("path");
var fs = require("fs");

module.exports = function(config){
    return function(app_module){
        app_module.deployer = build_deployer(config,app_module);
    }
}

function build_deployer(config,app_module){
    var app_config = app_module.config;

    var templates = {};
    

    function write_styles(styles){
        var style_src = [];
        (app_config.styles||[]).forEach(function(style) {
            if(config.style_alias[style]){
                style = config.style_alias[style];
            }
            style_src.push("<link rel='stylesheet' href='"+style+"'></link>");
            
        });

        return style_src.join("\n");
    }

    function get_cpmt_path(app_name,cpmt_name){
        return "/platform/"+app_name+"/"+(app_config.component_dir||config.component_dir)+"/"+cpmt_name+".js";
    }

    function get_view_path(app_name,cpmt_name){
        return "/"+app_name+"/"+(app_config.view_dir||config.view_dir)+"/"+cpmt_name+".html";
    }

    function write_scripts(){
        var script_src = [];

        (config.base_lib||[]).forEach(function(script) {
            if(config.script_alias[script]){
                script = config.script_alias[script];
            }
            script_src.push("<script type='text/javascript' src='"+script+"'></script>");
        });

        
        //console.log(script_src);

        (app_config.libs||[]).forEach(function(script) {
            if(config.script_alias[script]){
                script = config.script_alias[script];
            }
            script_src.push("<script type='text/javascript' src='"+script+"'></script>");
        });

        //console.log(script_src);

        (app_config.components||[]).forEach(function(cpmt) {
            var cpmt_path;
            if(config.script_alias[cpmt]){
                cpmt_path = config.script_alias[cpmt];
            }else{
                cpmt_path = get_cpmt_path(app_module.app_name,cpmt);
            }
            script_src.push("<script type='text/javascript' src='"+cpmt_path+"'></script>");

            var view_path = get_view_path(app_module.app_name,cpmt);


            var view_content = fs.readFileSync(path.join(config.root_dir,app_module.app_root || app_module.platform_root ,view_path),'utf-8');
            script_src.push(view_content);

        });

        //console.log(script_src);

        return script_src.join("\n");
    }

    /*
    function write_template(){
        template
        var script_src = [];
        (app_config.libs||[]).forEach(function(script) {
            if(config.script_alias[script]){
                script = config.script_alias[script];
            }
            script_src.push("<script type='text/javascript' src='"+script+"'></script>");
        });

        (app_config.components||[]).forEach(function(cpmt) {
            if(config.script_alias[cpmt]){
                cpmt = config.script_alias[cpmt];
            }else{
                cpmt = get_cpmt_path(app_module.app_name,cpmt);
            }
            script_src.push("<script type='text/javascript' src='"+cpmt+"'></script>");
        });

        //console.log(script_src.join("\n"));

        return script_src.join("\n");
    }*/

    return  {
        write_styles:write_styles,
        write_scripts:write_scripts
    }
}
